<?php

namespace App\Api\Validate;

use App\Models\RoomSendEva;
use App\Models\RoomSendHis;
use Illuminate\Support\Facades\Validator;

class ComplaintsValidate extends BaseValidate
{

    public function add($params)
    {
        $rules = [
            'content' => 'required',
            'images'  => 'array'
        ];

        $messages = [
            'review.required'           => '评价不能为空~',
            'images.array'              => '图片格式不支持'
        ];
        $validator = Validator::make($params, $rules, $messages);

        $this->check($validator);
    }

    public function recordCheck($params)
    {
        $list = RoomSendHis::where('id', $params['id'])->get();
        if (empty($list)) {
            $this->return('订单不存在');
        }

        foreach ($list as $item) {
            if ($item->send_status == 2) {
                $this->return('订单已评价');
            }
            if (!$item->send_status) {
                $this->return('订单未完成，暂不能评价');
            }
            if ($item->is_user_eva) {
                $this->return('已评价完成');
            }
        }
    }


    /**
     * 送物订单：判断订单是否完成或已评价
     * @param $params
     * @param $living
     * @return mixed
     * @throws \Exception
     */
    public function checkRoomSendHis($params, $living)
    {
        if (array_key_exists('rsh_id', $params) && $params['rsh_id']) {
            $list = RoomSendHis::where('id', $params['rsh_id'])->get();
            if (empty($list->toArray())) {
                $this->return('订单不存在');
            }
            foreach ($list as $item) {
                if ($item->rse_id) {
                    $this->return('评价已完成');
                }
                if ($item->send_status == 2) {
                    $this->return('订单已取消，无法评价');
                }
            }
        } else {
            $where = [
                ['openid', $params['openid']],
                ['rse_id', 0],
                ['send_status', '<>',  2],
                ['order_at', '>=', $living->in_date . ' 00:00:00']
            ];

            $list = RoomSendHis::where($where)->get();
            if (empty($list->toArray())) {
                $this->return('没有评价订单');
            }
        }

        $ids = [];
        foreach ($list as $item) {
            if (!$item->send_status) {
                $this->return('订单未完成，暂不能评价');
            }
            $ids[] = $item->id;
        }

        return $ids;
    }

    public function detail($id)
    {
        if (!$id) {
            $this->return('参数错误');
        }

        $record = RoomSendEva::find($id);
        if (!$record) {
            $this->return('评价不存在');
        }

        if (!$record->status) {
            $this->return('评价已删除');
        }
        return $record;
    }
}
